package com.unohacha.qqj.fzdr.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.unohacha.qqj.fzdr.bean.Constants;
import com.unohacha.qqj.fzdr.util.BroadCastReceiverUtil;
import com.unohacha.qqj.fzdr.util.Prefer;
import com.unohacha.qqj.fzdr.util.ToastUtils;


public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = "WXPayEntryActivity";
    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.pay_result);
        api = WXAPIFactory.createWXAPI(this, "wx4b7c35ff36483d1c");
        api.handleIntent(getIntent(), this);
    }


    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {

    }

    //处理微信支付回调
    @Override
    public void onResp(BaseResp resp) {
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            if ("0".equals(String.valueOf(resp.errCode))) {
                ToastUtils.showToast(WXPayEntryActivity.this, "支付成功");
                Log.e("===微信回调===2", "---------------------------------------------------" + Prefer.getInstance().getWxSUCfrom());
                if(!Prefer.getInstance().getWxSUCfrom().equals("")){
                    if(Prefer.getInstance().getWxSUCfrom().equals("TG")){
                        BroadCastReceiverUtil.sendBroadcast(this, Constants.ACTION_WEIXINPAY_SUCCRESS_TGMAKEORDERACTIVITY);
                    }else if(Prefer.getInstance().getWxSUCfrom().equals("LD")){
                        BroadCastReceiverUtil.sendBroadcast(this,Constants.ACTION_WEIXINPAY_SUCCRESS_LDORDERCONFIRMACTIVITY);
                    }else if(Prefer.getInstance().getWxSUCfrom().equals("PAY")){
                        BroadCastReceiverUtil.sendBroadcast(this,Constants.ACTION_WEIXINPAY_SUCCRESS_PAYACTIVITY);
                    }else if(Prefer.getInstance().getWxSUCfrom().equals("CMS")){
                        BroadCastReceiverUtil.sendBroadcast(this,Constants.ACTION_WEIXINPAY_SUCCRESS_CARTMAKESUREACTIVITY);
                    }
                }
            } else if ("-1".equals(String.valueOf(resp.errCode))) {
                ToastUtils.showToast(WXPayEntryActivity.this, "支付失败");
            } else if ("-2".equals(String.valueOf(resp.errCode))) {
                ToastUtils.showToast(WXPayEntryActivity.this, "取消支付");
            }
            finish();
        }
    }
}